package adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import gov.kqqzyyy.monkey.ydhl.R;
import objects.CustomExpandableListView;
import objects.HE_XJXM;
import objects.HE_XJYS;

/**
 * Created by Monkey on 2017/4/10.
 * 宣教样式（外层）
 */

public class XJYSListAdapter extends BaseExpandableListAdapter {

    private List<HE_XJYS> mXJYSList;
    private Context mContext;
    private LayoutInflater inflater;
    private List<XJXMListAdapter> adapters;
    private List<List<Boolean>> mIsGroupExpand;

    public XJYSListAdapter(Context context) {
        this.mContext = context;
        inflater = LayoutInflater.from(mContext);
    }

    public void setmXJYSList(List<HE_XJYS> mXJYSList) {
        this.mXJYSList = mXJYSList;
        Collections.sort(mXJYSList, (arg0, arg1) -> arg0.getYSBS().compareTo(arg1.getYSBS()));
        adapters = new ArrayList<>();
        mIsGroupExpand = new ArrayList<>();
        for (int i = 0; i < mXJYSList.size(); i++) {
            adapters.add(new XJXMListAdapter(mContext));
            List<Boolean> list = new ArrayList<>();
            for (int j = 0; j < mXJYSList.get(i).getList().size(); j++) {
                list.add(false);
            }
            mIsGroupExpand.add(list);
        }
        notifyDataSetChanged();
    }

    @Override
    public int getGroupCount() {
        if (mXJYSList != null && mXJYSList.size() > 0) {
            return mXJYSList.size();
        }
        return 0;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return 1;
    }

    @Override
    public Object getGroup(int groupPosition) {
        if (mXJYSList != null && mXJYSList.get(groupPosition) != null) {
            return mXJYSList.get(groupPosition);
        }
        return null;
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        if (mXJYSList != null && mXJYSList.get(groupPosition) != null && mXJYSList.get(groupPosition).getList() != null) {
            return mXJYSList.get(groupPosition).getList();
        }
        return null;
    }

    @Override
    public long getGroupId(int groupPosition) {
        return 0;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return 0;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        ParentViewHolder holder;
        if (convertView == null) {
            holder = new ParentViewHolder();
            convertView = inflater.inflate(R.layout.item_xjys_parent, parent, false);
            holder.textView = convertView.findViewById(R.id.xjys_text);
            convertView.setTag(holder);
        } else {
            holder = (ParentViewHolder) convertView.getTag();
        }
        if (mXJYSList != null && mXJYSList.size() > 0) {
            HE_XJYS heXjys = mXJYSList.get(groupPosition);
            if (heXjys != null) {
                String ysmc = heXjys.getYSMC() == null ? "" : heXjys.getYSMC();
                holder.textView.setText(ysmc);
            }
        }
        return convertView;
    }

    @Override
    public View getChildView(final int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        final ChildViewHolder holder;
        if (convertView == null) {
            holder = new ChildViewHolder();
            convertView = inflater.inflate(R.layout.item_xjys_child, parent, false);
            holder.customExpandableListView = convertView.findViewById(R.id.custon_expandable_lv);
            convertView.setTag(holder);
        } else {
            holder = (ChildViewHolder) convertView.getTag();
        }
        final XJXMListAdapter adapter = adapters.get(groupPosition);
        List<HE_XJXM> mXJXMList;
        if (mXJYSList != null && mXJYSList.get(groupPosition) != null && mXJYSList.get(groupPosition).getList() != null) {
            mXJXMList = mXJYSList.get(groupPosition).getList();
            adapter.setmXJXMList(mXJXMList);
            holder.customExpandableListView.setAdapter(adapter);
            for (int i = 0; i < mXJXMList.size(); i++) {
                if (mIsGroupExpand.get(groupPosition).get(i)) {
                    holder.customExpandableListView.expandGroup(i);
                }
            }
        }
        holder.customExpandableListView.setOnItemLongClickListener((parent12, view, position, id) -> {
            final long packedPos = holder.customExpandableListView.getExpandableListPosition(position);
            final int groupPos = ExpandableListView.getPackedPositionGroup(packedPos);
            final int childPos = ExpandableListView.getPackedPositionChild(packedPos);
            if (childPos == -1) {//长按的是group的时候，childPosition = -1
                adapter.setIsAllSelectMode(groupPos, true);
                boolean isAllSelect = !adapter.getIsParentChoiced().get(groupPos);
                adapter.setIsParentChoiced(groupPos, isAllSelect);
                if (!holder.customExpandableListView.isGroupExpanded(groupPos)) {
                    holder.customExpandableListView.expandGroup(groupPos);
                }
                adapter.notifyDataSetChanged();
                adapter.doSetDataSet(groupPos);
            }
            return true;
        });
        holder.customExpandableListView.setOnChildClickListener((parent1, v, groupPosition1, childPosition1, id) -> {
            boolean isChecked = !adapter.getIsChildChoiced().get(groupPosition1).get(childPosition1);
            adapter.changeChildChoiced(groupPosition1, childPosition1, isChecked);
            adapter.notifyDataSetChanged();
            return false;
        });
        holder.customExpandableListView.setOnGroupClickListener((parent13, v, i, id) -> {
            boolean isExpanded = !holder.customExpandableListView.isGroupExpanded(i);
            mIsGroupExpand.get(groupPosition).set(i, isExpanded);
            return false;
        });
        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    private class ParentViewHolder {
        private TextView textView;
    }

    private class ChildViewHolder {
        private CustomExpandableListView customExpandableListView;
    }


}
